var invalidWord;
(async function(){
    let data = await $.get('ajax/getInvalidword.php');
    invalidWord = JSON.parse(data).data;
    
})()
function tab(obj){
    if(event.keyCode==9){
        obj.value=obj.value+'    ';
        if(event && event.preventDefault){
            event.preventDefault();
        }else{
            window.event.returnValue=false;
        }
    }
}
//悬浮显示
function showtip(msg){
    let hover_tip = $('#hover-tip');
    hover_tip.text(msg);
    hover_tip.fadeIn();
    setTimeout(function(){
        hover_tip.fadeOut();
    },1000)
}
//   悬浮去顶部 去底部
var hover_btn = $('#hover-btn div');
var timer = null;
hover_btn.click(function(e){
    clearInterval(timer)
    e = e || window.e;
    let flag = $.trim($(e.target).attr('to'));
    let full_s = document.documentElement.offsetHeight - document.documentElement.clientHeight;
    let danwei_s = full_s/30;
    let top = $(window).scrollTop();
    danwei_s = flag === 'top' ? -danwei_s : danwei_s;
    timer = setInterval(function(){
        let sign = top;
        top += danwei_s;
        if(top < 0){
            top = 0;
            clearInterval(timer)
        }else if(top > full_s){
            top = full_s;
            clearInterval(timer)
        }
        $(window).scrollTop(top);
    },10)
});
//点击复制
function copyUrl(){
    url.select();
    if(document.execCommand('Copy')){
        showtip('已复制')
    }
}
// 表单验证
var fj_num = $('#glfj>b');
var send_btn = $('#send-btn');
var sendcontent = $('#sendcontent');
sendcontent.keydown(function(){
    $(this).removeClass('err')
});
send_btn.click(function(e){
    
    if(fj_num.text() == 0 && $.trim(sendcontent.val()) === ''){
        sendcontent.focus()
        sendcontent.addClass('err')
        e ? e.preventDefault() : window.e.returnValue = false;
    }else if(invalidWord.some(d=>sendcontent.val().indexOf(d) !== -1)){
        e ? e.preventDefault() : window.e.returnValue = false;
        alert('输入内容不能包含敏感词汇！')
    }

});
//点击关注
var follow = $('b.follow');
follow.click(function() {

    let that = $(this);
    that.toggleClass('followed');
    $.ajax({
        url: 'ajax/follow.php',
        type: 'GET',
        dataType: 'json',
        data: {w: 'follow',id: that.prop('id')},
    })
    .done(function(data) {
        var msg = data.sign == 'in'?'已关注':'已取关';
        showtip(msg)
    });
});
var glfj = $('#glfj');
var x = $('#x');
var up_list = $('#upload-zone');
glfj.click(function(event) {
     up_list.show()
});
x.click(function(event) {
    up_list.hide()
});
//文件上传
var file = $('#img');
var reader = new FileReader();
file.change(function(event) {
    if(!this.files.length || !/^image\/\w+/.test(this.files[0].type)){
        return
    }else{
        let filename = this.files[0].name;
        reader.readAsDataURL(this.files[0]);
        reader.onload=function(){
            $.ajax({
                url: 'ajax/upload.php',
                type: 'POST',
                cache: false, //不缓存上传文件
                data: {type: 0,files:this.result}
            })
            .done(function(data) {
                fj_num.text(parseInt(fj_num.text())+1);
                up_list.append(`<li><span class="file-name">${filename}</span><span onclick="del(this)" class="file-action hand">删除</span><input type="hidden" name="files[]" value="${data.file}"></li>`);
            });
        }
    }
});
function del(el){
    let delfile = $(el).next().val();
     $.ajax({
        url: 'ajax/upload.php',
        type: 'POST',
        data: {type: 'cancle',file: delfile}
    })
    .done(function() {
        $(el).parent().remove();
        fj_num.text(parseInt(fj_num.text())-1);
    });
}


$('.attachment-img>img').data('scale', true);
$('#showimg>img').data('scale', true);
$('.main').click(function(e) {
        if(!$(e.target).data('scale') && !$(e.target).hasClass('col') && !$(e.target).hasClass('rep')){
           return 
        }
        if($(e.target).data('scale')){ //图片放大
            let img = new Image();
            img.src = $(e.target).prop('src');
            let img_w = img.width;
            let img_h = img.height;
            let max_w = $(window).width()*0.9;
            let max_h = $(window).height()*0.95;

            img_w = img_w>max_w ? max_w:img_w;
            img_h = img_h>max_h ? max_h:img_h;
            $('#showimg').css({
                width: img_w,
                height: img_h
            });
            $('#meng').toggle();
            $('#showimg>img').prop('src', $(e.target).prop('src'))
        }else if($(e.target).hasClass('col')){ //收藏

            $(e.target).next().toggleClass('collected');
            $.ajax({
                url: 'ajax/follow.php',
                type: 'GET',
                dataType: 'json',
                data: {w: 'content',id: $(e.target).prop('id')},
            })
            .done(function(data) {
                let msg = data.sign == 'in'?'已收藏':'已取消';
               showtip(msg)
            });
        }else{
            $('#report').show();
            $('#report').data('id', $(e.target).prop('id'))
        }
        
});
$('#report').click(function(e) {
    if(!$(e.target).hasClass('btn')){
        return
    }
    if($(e.target).prop('id') == 'sure'){
        let str = '';
        let len = $(':checked').length;
        for(let i=0;i<len;i++){
            str += $(':checked')[i].value;
            if(i != len-1){
                str += ','
            }
        }
        let reason = $('#reason').val();
        $.ajax({
            url: 'ajax/report.php',
            type: 'POST',
            data: {id: $(this).data('id'),str,reason},
        })
        .done(function() {
            $('#report').hide();
            showtip('举报成功');
            $(':checked').prop('checked',false);
            $('#reason').val('');
        });
    }else{
        $(this).hide();
    }
});
//表情包
$('#emoji').click(function() {
    $(this).toggleClass('sel-bd');
    $('.bq-zone').toggle();
});
function addsmile(smilecode){
    let body = $('#sendcontent');
    let original_val = body.val();
    body.val(original_val + smilecode);
    body.focus();
}
var tip = $('#tip');
if(tip.length){
    tip.css('top','60px')
    setTimeout(function(){
        tip.css('top','-5px')
        setTimeout(function(){
            let href = window.location.href,rand = parseInt(Math.random()*1000);
            href = href.replace(/&rand=\d+/,'&rand='+rand)
            window.location.replace(href)
        },500)
       
    },1000)
}